//
//  GHAboutUsViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/16.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMGPersonalCenterController.h"
#import "TMGEditPersonInfoViewController.h"
#import "TMGRealNameViewController.h"
#import "STCardMangerController.h"
#import "STDidRealNameViewController.h"
@interface TMGPersonalCenterController ()
@property(nonatomic, strong) NSArray<STTableEasyModel*>                     *datasouce;
@property(nonatomic, strong) TMGUserModel                     *userModel;
@end

@implementation TMGPersonalCenterController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"";
    [self st_setRightItemWithTitle:@"编辑" titleColor:[UIColor whiteColor]];
    [self configSubView];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendUserInfoRequest];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    NSString * gender;
    if (self.userModel.gender.integerValue == 1) {
        gender = @"男";
    }else if (self.userModel.gender.integerValue == 0){
        gender = @"女";
    }else{
        gender = @"";
    }
    NSString * area = [NSString stringWithFormat:@"%@%@",self.userModel.province,self.userModel.city];
    self.datasouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"黄金口袋账号"
                                                       detailString:self.userModel.mobile accessoryType:UITableViewCellAccessoryNone],
                       [[STTableEasyModel alloc] initWithTextString:@"身份认证" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"银行卡" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"性别"
                                                       detailString:gender accessoryType:UITableViewCellAccessoryNone],
                       
                       [[STTableEasyModel alloc] initWithTextString:@"地区"
                                                       detailString:area accessoryType:UITableViewCellAccessoryNone]
                       ];
    
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 190)];
    header.backgroundColor = self.tableView.backgroundColor;
    
    UIImageView * logImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 35, 80, 80)];
    logImageView.clipsToBounds = YES;
    logImageView.layer.cornerRadius = 5;
    logImageView.st_centerX = UIScreenWidth / 2;
    logImageView.image = [UIImage imageNamed:@"消息头像"];
    //    logImageView.backgroundColor = [UIColor colorWithRed:55/255.0 green:60/255.0 blue:156/255.0 alpha:1];
    [header addSubview:logImageView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, logImageView.st_bottom + 5, 120, 18)
                                                     text:[NSString stringWithFormat:@"----\n---------"]
                                                textColor:TM_secendTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.st_centerX = UIScreenWidth / 2;
    titleLable.numberOfLines = 0;
    [NSString lableAutoAdjustheightWithLabel:titleLable];
    [header addSubview:titleLable];
    if (self.userModel) {
        [logImageView sd_setImageWithURL:[NSURL URLWithString:self.userModel.head] placeholderImage:nil];
        titleLable.text = [NSString stringWithFormat:@"%@\n%@",self.userModel.nickname,self.userModel.mobile];
    }
    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datasouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    STTableEasyModel * model = self.datasouce[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [self.navigationController pushViewController:[TMGRealNameViewController new] animated:YES];
//    return;
    if (indexPath.row == 1) {
        if (self.userModel.is_verify) {
            STDidRealNameViewController * vc = [STDidRealNameViewController new];
            vc.userModel = self.userModel;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
             [self.navigationController pushViewController:[TMGRealNameViewController new] animated:YES];
        }
       
    }
    if (indexPath.row == 2) {
        [self.navigationController pushViewController:[STCardMangerController new] animated:YES];
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)st_rightBarAction:(id)sender{
    TMGEditPersonInfoViewController * vc =  [TMGEditPersonInfoViewController new];
    vc.userModel = self.userModel;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)sendUserInfoRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setValue:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
         [dic setValue:[STUserManger defult].token forKey:@"token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/getPersonal"];
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 TMGUserModel * model = [TMGUserModel mj_objectWithKeyValues:dataDic];
                                                 self.userModel = model;
                                                 [self configSubView];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                             }];
}
@end


